<?php

namespace common\models\datas;

use common\components\behaviors\UpdateCacheBehavior;
use Yii;
use yii\helpers\ArrayHelper;
use common\components\helpers\EventData;
use common\components\helpers\Util;
use common\components\extenders\MenuManage;

/**
 * This is the model class for table "{{%message_config}}".
 *
 * @property integer $id
 * @property string $name
 * @property integer $event_id
 * @property string $variable
 */
class MessageConfig extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%message_config}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'event_id', 'variable', 'send_group','mode'], 'required'],
            [['event_id', 'type_id','mode'], 'integer'],
            [['variable', 'send_group'], 'string'],
            [['name'], 'string', 'max' => 255]
        ];
    }

    public function fields()
    {
        $field = parent::fields();
        $field['event_id'] = function () {
            return (new EventData())->getNameByKey($this->event_id);
        };
        $field['type_id'] = function () {
            return (new EventData())->getTypeByKey($this->type_id);
        };
        $field['send_group'] = function () {
            return (new EventData())->getSendGroupByKey($this->send_group);
        };
        $field['variable'] = function () {
            //$v = Util::array2text(unserialize($this->variable));
         //   $v = nl2br($v);
         //   return $v;
        };
        $field['mode'] = function(){
            switch ($this->mode){
                case 1:
                    return \Yii::t('common','共同体');
                case 2:
                    return \Yii::t('common','综合体');
                case 3:
                    return \Yii::t('common','管玄乐');
                default :
                    return \Yii::t('common','');
            }
    };
        return $field;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自动编号'),
            'name' => Yii::t('common', '消息名称'),
            'event_id' => Yii::t('common', '事件编号'),
            'type_id' => Yii::t('common', '类型编号'),
            'variable' => Yii::t('common', '参数'),
            'send_group' => Yii::t('common', '发送对象'),
            'mode' => Yii::t('common', '模式'),
        ];
    }

    public function extraFields()
    {
        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();

            },
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => UpdateCacheBehavior::className(),
                'cacheName' => 'messageconfig'
            ]
        ]);
    }
}
